package com.sophialex.health.record.lktable.join;

import java.util.Date;
import java.util.Iterator;
import java.util.Set;

import org.apache.commons.httpclient.util.DateUtil;

import com.sophia.dict.vo.DictCode;
import com.sophialex.health.common.platform.SuperContext;
import com.sophialex.health.record.common.utils.CommonUtils;
import com.sophialex.health.system.lktable.LKIGetJoinOutValue;

/**
 * 
 * <p>Title:Health</p>
 * <p>FamilyJoinOutValue</p>
 * 
 * Alex Apr 28, 2011 9:53:41 AM create
 * @author <a href='wanghao924@126.com'>Alex</a>
 * QQ: 873987060;
 * @version 1.0
 */
public class FamilyJoinOutValue implements LKIGetJoinOutValue{

	@Override
	public String getFieldValue(Object obj, String fieldName, Object byValue) {
		if ("perRelaMaster".equals(fieldName)) { 
			 if(byValue!=null){
				 try {
					 Set<DictCode> set = SuperContext.dictCache.getCodesByName("RELATION");
					 Iterator it = set.iterator();
					 while(it.hasNext()){
						 DictCode dict = (DictCode) it.next();
						 if(dict.getCodeNum().equals(byValue.toString())){
							 return dict.getCodeValue();
						 }
					 }
				}
				catch(Exception e) {
					e.printStackTrace();
				}
			 }
		}
		if ("sex".equals(fieldName)) { 
			 if(byValue!=null){
				 try {
					 Set<DictCode> set = SuperContext.dictCache.getCodesByName("SEX");
					 Iterator it = set.iterator();
					 while(it.hasNext()){
						 DictCode dict = (DictCode) it.next();
						 if(dict.getCodeNum().equals(byValue.toString())){
							 return dict.getCodeValue();
						 }
					 }
				}
				catch(Exception e) {
					e.printStackTrace();
				}
			 }
		}
		if ("birthday".equals(fieldName)) { 
			 if(byValue!=null && !"".equals(byValue)){
				 try {
					 Date date = new Date(Date.parse(byValue.toString()));
					 return DateUtil.formatDate(date,"yyyy-MM-dd");
				}
				catch(Exception e) {
					e.printStackTrace();
				}
			 }
		}
		if ("adminUserId".equals(fieldName)) { 
			 if(byValue!=null && !"".equals(byValue)){
				 try {
					 
					 return CommonUtils.getUserNameById(Long.valueOf(byValue.toString()));
				}
				catch(Exception e) {
					e.printStackTrace();
				}
			 }
		}
		return byValue == null ? "" : byValue.toString();
	}

}
